<?php
//----------------------------------------------------------------------------//
// Class              : Ctl_CatSanpham        							  //
// Modification       :														  //
//----------------------------------------------------------------------------//
// Date(yyyy.mm.dd)   Developer                       Action				  //
//----------------------------------------------------------------------------//
// 2007.03.28         Oanhhtk                     Created				  //
//----------------------------------------------------------------------------//

class Ctl_CatSanpham extends ControlBase
{
    var $modul="catSanpham";
	//==========================================================================
	// Function    : Ctl_CatContent
	// Argument    :
	// ReturnValue :
	// Creator     : Oanhhtk  2007.03.28
	// Details     : AppendData record of table
	//==========================================================================
	function Ctl_CatSanpham()
	{
	   	 ControlBase::ControlBase();
	}
	//==========================================================================
	// Function    :  execute
	// Argument    :
	// ReturnValue :
	// Creator     : Oanhhtk  2007.03.28
	// Details     : AppendData record of table
	//==========================================================================
 	function execute($action="")
	{
	    global $db;
	    global     $arrayError,$arrayData_parent_id0,$arrayData_parent_id,$_POST;
	    $arrayData =  array();
		$arrayData_parent_id0 = array();
		$arrayData_parent_id= array();
	    $this->modul = CM_getParameter(MODUL);
 		if ($action==""){
 			$action = CM_getParameter(ACTION);
 		}
		// Get data
		include_once(DIR_MODEL."md_cat_san_pham.class.php");
		$objModel = new MD_CatSanpham($this->db);
 		switch($action)
		{
			case "Add":
				if(!isset($_POST['Submit']))
				{
                   // show form add new
       				include_once(DIR_VIEW."v_cat_san_pham_add.php");
					V_CatSanphamAdd($arrayData);
				}
				else
				{
					// check validating
                    if($this->validate() && $this->insert())
					{
                     	CM_gotoPage("?".MODUL."=".$this->modul."&".ACTION."=list");
                    }
                    else
					{
         				include_once(DIR_VIEW."v_cat_san_pham_add.php");
						V_CatSanphamAdd(&$_POST);
                    }

				}
				break;
			case "Delete":
				$id = CM_getParameter("id");
				include_once(DIR_MODEL."md_san_pham.class.php");
				$objModel_Sanpham = new MD_Sanpham($this->db);
				//loai san pham
				$arrayDataSanpham = $objModel_Sanpham->getByWhere("*","id_loai_sp=".$id."","");
				//san pham
				$arrayData = $objModel->getByWhere("*","parent_id=".$id."","");
				//download driver
				include_once(DIR_MODEL."md_download_driver.class.php");
				$objModel_Download = new MD_DownloadDriver($this->db);
				$arrayData_Download = array();
				$arrayData_Download = $objModel_Download->getByWhere("*","id_danh_muc =".$id."","");
				
				if(count($arrayDataSanpham)>0 || count($arrayData)>0 || count($arrayData_Download)>0){
					echo "<script>alert('Xin lỗi,Bạn phải xóa hết các bản ghi bên trong thuộc loại bản ghi này hoặc các file download driver thuộc loại này trước khi xóa nó.');</script>";
					CM_gotoPage("?".MODUL."=".$this->modul."&".ACTION."=list");   
				}else{
					$this->deleteById($id);
					CM_gotoPage("?".MODUL."=".$this->modul."&".ACTION."=list");   
				}          
				break;
			case "Edit":
				include_once(DIR_VIEW."v_cat_san_pham_add.php");
 				$id = CM_getParameter("id");
				if(isset($_POST['Submit']))
				{
                    if($this->update())
					{
						CM_gotoPage("?".MODUL."=".$this->modul."&".ACTION."=list");
                    }
                    else
					{
                    	$arrayData =  &$_POST;
						V_CatSanphamAdd($arrayData,"Edit");
                    }
				}
				else
				{
                     $arrayData = $objModel->getById2($id);
                     if(empty($arrayData))
					 {
                         $arrayError[]="Không tìm thấy bản ghi nào."   ;
                     }
                     V_CatSanphamAdd($arrayData,"Edit");
                    
				}
				break;	
			case "View":
				include_once(DIR_VIEW."v_cat_san_pham_view.php");
 				$id = CM_getParameter("id");
                     $arrayData = $objModel->getById2($id);
                     if(empty($arrayData))
					 {
                         $arrayError[]="Không tìm thấy bản ghi nào."   ;
                     }
                     V_CatSanphamAdd($arrayData,"View");
                    
				break;		
			case "ViewHomePage":
				
				$arrayData = $_POST['element'];
				//print_r($arrayData);
				//die;
				if(isset($_POST['Submit'])){
					
					//Update QuynhTTN 2009.10.05 Start
					//if($this->UpdateHomePage($arrayData)){
					if($this->UpdateHomePage($arrayData)&& $this->UpdateNotHomePage($arrayData)){
					//Update QuynhTTN 2009.10.05 End
						CM_gotoPage("?".MODUL."=".$this->modul."&".ACTION."=list");
					}
				}
				break;			
			case "Edit_ht":  
                $strID = CM_getParameter("id");   
                $strHienThi = CM_getParameter("hien_thi");   
                // Update data
                if (!$this->update_ht($strID,$strHienThi)){
                    $arrayError[]= " Có lỗi trong quá trình cập nhật dữ liệu.";
                } 
			case "list":
			default : 
			{
				$arrayData_parent_id0 = $objModel->getByWhere("*","parent_id=0","");
				$arrayData_parent_id = $objModel->getByWhere("*", "parent_id<>0","");
				//$arrayData = $objModel->getByWhere("id,ten_loai_sp,thuoc_tinh_sp,thu_tu,hien_thi","","id desc");
                 // show data
				include_once(DIR_VIEW."v_cat_san_pham_list.php");
				//check loi
				if ($objModel->errno())
				{
           			  $arrayError[]= $objModel->error();
            	}
				V_CatSanphamList($arrayData_parent_id0,$arrayData_parent_id);
			}
 		}
        unset($arrayData_parent_id0,$arrayData_parent_id);
	}
	//==========================================================================
    // Function    : update_ht                                                   //
    // Creator     : Oanhhtk 2007.03.29                                        //
    // Details     : Update hien_thi field                                    //
    //==========================================================================
    function update_ht($strID, $strHienThi)
	{
        //global $arrayError, $_FILES;
		
		global     $arrayError,$_POST;
		include_once(DIR_MODEL."md_cat_san_pham.class.php");  
   		$objModel = new MD_CatSanpham($this->db);
        $objModel->update_ht($strID, $strHienThi);
        if ($objModel->errno()){
            $arrayError[]= $objModel->error();
            return false;
        }
        return true;
    }  
	
	function UpdateHomePage($arrayData){
		
		global     $arrayError,$_POST;
		include_once(DIR_MODEL."md_cat_san_pham.class.php");  
   		$objModel = new MD_CatSanpham($this->db);
        $objModel->UpdateHomePage($arrayData);
        if ($objModel->errno()){
            $arrayError[]= $objModel->error();
            return false;
        }
        return true;
	}
	
	function UpdateNotHomePage($arrayData){
		
		global     $arrayError,$_POST;
		include_once(DIR_MODEL."md_cat_san_pham.class.php");  
   		$objModel = new MD_CatSanpham($this->db);
        $objModel->UpdateNotHomePage($arrayData);
        if ($objModel->errno()){
            $arrayError[]= $objModel->error();
            return false;
        }
        return true;
	}

	//==========================================================================
	// Function    : validate
	// Argument    :
	// ReturnValue :
	// Creator     : Oanhhtk 2007.03.28
	// Details     : AppendData record of table
	//==========================================================================
 	function validate()
	{
        global     $arrayError,$_POST;
   		if (trim($_POST["ten_loai_sp"])=="") 
		{
   			$arrayError[]="Tên loại sản phẩm không được rỗng.";
   			return false;
   		}
        return true;
	}
 	//==========================================================================
	// Function    :  deleteById
	// Argument    :
	// ReturnValue :
	// Creator     : Oanhhtk  2007.03.28
	// Details     : AppendData record of table
	//==========================================================================
 	function deleteById()
	{
        global     $arrayError,$_POST;
   		$objModel = new MD_CatSanpham($this->db);
   		$objModel->deleteById(CM_getParameter("id"));
     	if ($objModel->errno())
		{
             $arrayError[]= $objModel->error();
             return false;
     	}
     	return true;
        //return true;
	}
	//==========================================================================
	// Function    :  update
	// Argument    :
	// ReturnValue :
	// Creator     : Oanhhtk  2007.03.28
	// Details     : AppendData record of table
	//==========================================================================
 	function update()
	{
         global     $arrayError,$_POST;
		include_once(DIR_MODEL."md_cat_san_pham.class.php");  
   		$objModel = new MD_CatSanpham($this->db);
   		$objModel->update(&$_POST);
     	if ($objModel->errno())
		{
             $arrayError[]= $objModel->error();
             return false;
     	}
		
			
		$dir = "../images/images_upload/img_con_sp/";
		$this->DoUploadFileImg($dir);
		return true;
	}
	
	//==========================================================================
	// Function    :  showList
	// Argument    :
	// ReturnValue :
	// Creator     : Tran Thanh 2007.03.28
	// Details     : AppendData record of table
	//==========================================================================
	function insert()
	{
        global     $arrayError,$_POST;
		include_once(DIR_MODEL."md_cat_san_pham.class.php");  
   		$objModel = new MD_CatSanpham($this->db);
   		$objModel->insert(&$_POST);
     	if ($objModel->errno())
		{
             $arrayError[]= $objModel->error();
             return false;
     	}
		//-----------------Goi ham upaloadfile ---------------------------------
		$dir = "../images/images_upload/img_con_sp/";
		$this->DoUploadFileImg($dir);
		return true;
        //return true;
	}
	//==========================================================================
	// Function    :  showList
	// Argument    :
	// ReturnValue :
	// Creator     : Oanhhtk  2007.03.28
	// Details     : AppendData record of table
	//==========================================================================
 	function showList()
	{
		
      	include_once(DIR_MODEL."md_cat_san_pham.class.php");
		$objModel = new MD_CatSanpham($this->db);
		$arrayData_parent_id0 = $objModel->getByWhere("*","parent_id=0","");
		
		$arrayData_parent_id = $objModel->getByWhere("*", "parent_id<>0","");
		
		
		 // show data
		include_once(DIR_VIEW."v_cat_san_pham_list.php");
		//check loi
		if ($objModel->errno())
		{
			  $arrayError[]= $objModel->error();
		}
		V_CatSanphamList($arrayData_parent_id0,$arrayData_parent_id);
	}

}
?>